package ip.ejb.entity;
/***********************************************************************
 * Module:  OrderBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class OrderBean
 ***********************************************************************/

import ip.ejb.sequence.SequenceSessionLocal;
import ip.ejb.sequence.SequenceSessionLocalHome;

import java.sql.Date;
import java.util.Collection;

import javax.ejb.CreateException;
import javax.ejb.EntityContext;
import javax.naming.InitialContext;

/**
 * @ejb.bean 
 * 	cmp-version="2.x"
 *  name="Order"
 *  type="CMP"
 *  jndi-name="ip/ejb/entity/Order"
 *  local-jndi-name="ip/ejb/entity/OrderLocal"
 *  reentrant="false"
 *  
 * @ejb.finder
 *   signature="java.util.Collection findAll()"
 *   query="SELECT OBJECT(a) FROM Order a"
 *   method-intf="LocalHome"
 *  
 * @ejb.persistence
 * 	table-name="order"
 * 
 * @ejb.transaction
 * 	type="Required"
 * 
 * @jboss.table-name
 * 	table-name="order"
 * 
 * @jboss.create-table
 * 	create="true"
 * 
 * @jboss.remove-table
 * 	remove="false"
 */
public abstract class OrderBean implements javax.ejb.EntityBean
{
	private EntityContext ejbContext = null;
	private SequenceSessionLocal sequenceSession = null;

	/**
	 * @ejb.create-method view-type="both"
	 */
	public OrderPK ejbCreate(UserLocal users, Date order_date, String ccNumber, int total_price) throws CreateException {

		setOrderId(sequenceSession.getNextValue("Order"));
		setOrderDate(order_date);
		setCreditCardNumber(ccNumber);
		setTotalPrice(total_price);

		return null;
	}   

	public void ejbPostCreate(UserLocal users, Date order_date, String ccNumber, int total_price)
	{
		setUser(users);
	}
	
	public void setEntityContext(EntityContext ctx) throws javax.ejb.EJBException
	{
		SequenceSessionLocalHome home = null;
		Object obj = null;
		this.ejbContext = ctx;
		try {
			obj = (new InitialContext()).lookup("ejb/util/SequenceSessionLocal");
			home = (SequenceSessionLocalHome) obj;
			sequenceSession = home.create();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void unsetEntityContext() throws javax.ejb.EJBException
	{
		   this.ejbContext = null;
		   try {
			   sequenceSession.remove();
		   } catch (Exception ex) {
			   ex.printStackTrace();
		   }
	}

	/**
	 * @ejb.persistence column-name="order_id"
	 * @ejb.interface-method view-type="both"
	 * @ejb.pk-field
	 * @jboss.jdbc-type type="INTEGER"
	 * @jboss.sql-type  type="INTEGER"
	 */
	public abstract int getOrderId();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrderId(int orderId);

	/**
	 * @ejb.persistence column-name="order_date"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="DATE"
	 * @jboss.sql-type  type="DATE"
	 */
	public abstract Date getOrderDate();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrderDate(Date orderDate);

	/**
	 * @ejb.persistence column-name="credit_card_number"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="VARCHAR"
	 * @jboss.sql-type  type="VARCHAR(16)"
	 */
	public abstract String getCreditCardNumber();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setCreditCardNumber(String creditCardNumber);

	/**
	 * @ejb.persistence column-name="total_price"
	 * @ejb.interface-method view-type="both"
	 * @jboss.jdbc-type type="DECIMAL"
	 * @jboss.sql-type  type="DECIMAL(8,2)"
	 */
	public abstract int getTotalPrice();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setTotalPrice(int totalPrice);
	
	/**
	 * @ejb.interface-method view-type="local"
	 * @ejb.relation
	 *   name="Order-User"
	 *   role-name="Order-of-User"
	 *   cascade-delete="yes"
	 * @jboss.relation
	 *   fk-constraint="true"
	 *   fk-column="userId"
	 *   related-pk-field="userId"
	 */
	public abstract UserLocal getUser();
	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setUser(UserLocal users);

	/**
	 * @ejb.interface-method view-type="local"
	 * @ejb.relation
	 *   name="OrderItem-Order"
	 *   role-name="OrderItems-from-Orders"
	 *   target-ejb="OrderItem"
	 */
	public abstract Collection getOrderItem();

	/**
	 * @ejb.interface-method view-type="local"
	 */
	public abstract void setOrderItem(Collection orderItems);
	
	public void ejbActivate() throws javax.ejb.EJBException{}
	public void ejbLoad() throws javax.ejb.EJBException{}
	public void ejbPassivate() throws javax.ejb.EJBException{}
	public void ejbRemove() throws javax.ejb.RemoveException, javax.ejb.EJBException{}
	public void ejbStore() throws javax.ejb.EJBException{}

}